﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Admin.IBLL;
using CNKI.TPI.Web.Admin.Model;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.BLL;
using CNKI.TPI.Web.Search.IBLL;
using CNKI.TPI.Web.Search.Model;
using KNet.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CNKI.TPI.Web.UI.Controllers.Admin
{
    public class Admin_HomeController : BaseController
    {
        //
        // GET: /Admin_Home/
        IHomeConfigService homeService = null;
        IMultiDBPublishService mService = null;
        public Admin_HomeController(IHomeConfigService homeService, IMultiDBPublishService mService)
        {
            this.homeService = generator.CreateInterfaceProxyWithTarget<IHomeConfigService>(homeService, new IInterceptor[] { new LogInterceptor() });
            this.mService = generator.CreateInterfaceProxyWithTarget<IMultiDBPublishService>(mService, new IInterceptor[] { new LogInterceptor() });
        }
        public ActionResult Index()
        {
            IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> list = homeService.GetHomepageConfig();
            ViewBag.ContentList = list;
            ViewBag.ContentCount = list.Count;
            ViewBag.JumpList = homeService.GetHomepageJumpConfig();
            return View();
        }
        public ActionResult AddHomeConfig()
        {
            ViewBag.Flage = "save";
            ViewBag.DBList = mService.GetPublishDBList(false);
            ViewBag.SysID = 0;
            //初始化
            ViewBag.DBValue = 0;
            ViewBag.SetNum = "";//设置显示多少条
            ViewBag.SetField = "";//设置字数
            ViewBag.MenuName = "";
            ViewBag.Menulength = "";
            ViewBag.ISPub = "1";
            ViewBag.Location = "0";
            ViewBag.TemplateType = "";
            return PartialView("AddHomeConfig");
        }
        public ActionResult UpdateHomeConfig(int sysid, int type)
        {
            ViewBag.Flage = "update";
            ViewBag.DBList = mService.GetPublishDBList(false);
            if (type == 1)//跳转模板
            {
                IList<SYS_CMS_NEWS_JUMPTEMPLATE> jumplist = homeService.GetHomepageJumpByID(sysid);
                if (jumplist != null && jumplist.Count > 0)
                {
                    ViewBag.DBValue = jumplist[0].DbID;
                    ViewBag.SetNum = jumplist[0].ArticleLength;//设置显示多少条
                    ViewBag.SetField = "";//设置字数
                    ViewBag.MenuName = jumplist[0].ColumnName;
                    ViewBag.Menulength = "";
                    ViewBag.ISPub = jumplist[0].IsPub;
                    ViewBag.Location = jumplist[0].Location.ToString();
                    ViewBag.TemplateType = jumplist[0].TemplateType;
                }
            }
            else
            {
                IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> list = homeService.GetHomepageConfigByID(sysid);
                if (list != null && list.Count > 0)
                {
                    ViewBag.DBValue = list[0].DbID;
                    ViewBag.SetNum = list[0].ArticleNum;//设置显示多少条
                    ViewBag.SetField = list[0].ArticleLength;//设置字数
                    ViewBag.MenuName = list[0].ColumnName;
                    ViewBag.Menulength = list[0].NameLength;
                    ViewBag.ISPub = list[0].IsPub.ToString();
                    ViewBag.Location = list[0].Location.ToString();
                    ViewBag.TemplateType = list[0].ShowType;
                }
            }

            ViewBag.SysID = sysid;
            return PartialView("AddHomeConfig");
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public int SaveDBConfig(FormCollection form)
        {
            int result = 0;
            string flage = "save";
            int type = 0;
            int dbid = 0;
            if (!String.IsNullOrEmpty(Request.Params["Flage"]))
            {
                flage = Request.Params["Flage"].ToString();
            }
            if (!String.IsNullOrEmpty(Request.Params["dblistValue"]))
            {
                string dbvalue = Request.Params["dblistValue"]; //// dbvalue[0]  数据库Id， dbvalue[1] 数据库类型
                dbid = int.Parse(dbvalue.Split(';')[0]);
            }
            if (!String.IsNullOrEmpty(Request.Params["obsertype"]))
            {
                type = int.Parse(Request.Params["obsertype"]);
            }

            if (flage == "save") //保存时判读验证
            {
                if (IsExistDBConfig(dbid, type))
                {
                    result = 2; //该数据库已经设置过该模板
                    return result;
                }
            }
            else //修改，修改判断时排除记录本身
            {
                int sysid = 0;
                if (!String.IsNullOrEmpty(Request.Params["SysID"]))
                {
                    sysid = int.Parse(Request.Params["SysID"]);
                }
                if (type == (int)ShowType.JumpTemplate || type == (int)ShowType.JumpTemplateText)
                {
                    if (homeService.IsExistJumpByID(dbid, type, sysid))
                    {
                        result = 2; //该数据库已经设置过该模板
                        return result;
                    }
                }
                else
                {
                    if (homeService.isExitsTemplateContent(dbid, type, sysid))
                    {
                        result = 2; //该数据库已经设置过该模板
                        return result;
                    }
                }

            }
            if (type == (int)ShowType.JumpTemplate || type == (int)ShowType.JumpTemplateText) //跳转模板
            {
                return saveJump(form, flage, type, dbid);
            }
            else
            {
                return saveContentNew(form, flage, type, dbid);
            }
        }
        /// <summary>
        /// 保存栏目信息
        /// </summary>
        /// <param name="form"></param>
        /// <param name="flage"></param>
        /// <param name="type"></param>
        /// <param name="dbid"></param>
        /// <returns></returns>
        private int saveContentNew(FormCollection form, string flage, int type, int dbid)
        {
            int result = 0;
            SYS_CMS_NEWS_NEWSCONTENTCONFIG obj = new SYS_CMS_NEWS_NEWSCONTENTCONFIG();
            obj.DbID = dbid;
            obj.ShowType = type;
            if (!String.IsNullOrEmpty(Request.Params["setnum"]))
            {
                obj.ArticleNum = int.Parse(Request.Params["setnum"]);
            }
            if (!String.IsNullOrEmpty(Request.Params["setfield"]))
            {
                obj.ArticleLength = int.Parse(Request.Params["setfield"]);
            }
            if (!String.IsNullOrEmpty(Request.Params["menuname"]))
            {
                obj.ColumnName = Request.Params["menuname"];
            }
            if (!String.IsNullOrEmpty(Request.Params["menulength"]))
            {
                obj.NameLength = int.Parse(Request.Params["menulength"]);
            }
            if (!String.IsNullOrEmpty(Request.Params["display"]))
            {
                obj.IsPub = int.Parse(Request.Params["display"]);
            }
            if (!String.IsNullOrEmpty(Request.Params["position"]))
            {
                obj.Location = int.Parse(Request.Params["position"]);
            }
            obj.RecommendSQL = "";
            obj.ImageURL = "";
            obj.BrifField = "";
            if (!String.IsNullOrEmpty(Request.Params["SysID"]))
            {
                obj.SysID = int.Parse(Request.Params["SysID"]);
            }
            if (homeService.SaveContentConfig(obj, flage))
            {
                result = 1;
            }
            return result;


        }
        /// <summary>
        /// 保存跳转栏目信息
        /// </summary>
        /// <param name="form"></param>
        /// <param name="flage"></param>
        /// <param name="type"></param>
        /// <param name="dbid"></param>
        /// <returns></returns>
        private int saveJump(FormCollection form, string flage, int type, int dbid)
        {

            int result = 0;
            SYS_CMS_NEWS_JUMPTEMPLATE obj = new SYS_CMS_NEWS_JUMPTEMPLATE();
            obj.DbID = dbid;
            obj.TemplateType = type;
            if (!String.IsNullOrEmpty(Request.Params["setnum"]))
            {
                obj.ArticleLength = int.Parse(Request.Params["setnum"]);
            }
            if (!String.IsNullOrEmpty(Request.Params["menuname"]))
            {
                obj.ColumnName = Request.Params["menuname"];
            }
            if (!String.IsNullOrEmpty(Request.Params["display"]))
            {
                obj.IsPub = Request.Params["display"].ToString();
            }
            if (!String.IsNullOrEmpty(Request.Params["position"]))
            {
                obj.Location = int.Parse(Request.Params["position"]);
            }
            obj.SortNmu = 0;
            if (!String.IsNullOrEmpty(Request.Params["SysID"]))
            {
                obj.SysID = int.Parse(Request.Params["SysID"]);
            }
            if (homeService.SaveContentJump(obj, flage))
            {
                result = 1;
            }
            return result;
        }

        /// <summary>
        /// 判断该数据库是否已经设置过
        /// </summary>
        /// <param name="dbid"></param>
        /// <param name="showtype"></param>
        /// <returns></returns>
        private bool IsExistDBConfig(int dbid, int showtype)
        {
            bool result = false;
            if (showtype == (int)ShowType.JumpTemplate || showtype == (int)ShowType.JumpTemplateText)
            {
                result = homeService.IsExistJumpByID(dbid, showtype);
            }
            else
            {
                result = homeService.isExitsTemplateContent(dbid, showtype);
            }
            return result;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sysid"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool DelContentConfig(int sysid, int type)
        {
            bool result = false;
            if (type == 1)
            {
                SYS_CMS_NEWS_JUMPTEMPLATE obj = new SYS_CMS_NEWS_JUMPTEMPLATE();
                obj.SysID = sysid;
                result = homeService.DeleteJump(obj);
            }
            else
            {
                SYS_CMS_NEWS_NEWSCONTENTCONFIG obj = new SYS_CMS_NEWS_NEWSCONTENTCONFIG();
                obj.SysID = sysid;
                result = homeService.DeleteContentNew(obj);
            }
            return result;
        }

        public ActionResult MenuIndex()
        {
            IList<SYS_CMS_NEWS_MENUCONFIG> MenuList = homeService.GetMenuList();
            ViewBag.MenuList = MenuList;
            ViewBag.MenuCount = MenuList.Count;
            return View("HomeMenuList");
        }
        public bool SaveMenu(SYS_CMS_NEWS_MENUCONFIG obj)
        {
            return homeService.SaveMenu(obj);
        }
        public bool DelMenu(int sysid)
        {
            return homeService.DelMenu(sysid);
        }
        public bool UpdateMenuSort(IList<SYS_CMS_NEWS_MENUCONFIG> list)
        {
            return homeService.UpdateMenuSort(list);
        }
        public bool UpdateHomeSort(IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> list)
        {
            return homeService.UpdateHomeSort(list);
        }

        public JsonResult GetMenuList()
        {
            IList<SYS_CMS_NEWS_MENUCONFIG> list = homeService.GetMenuList().OrderBy(c=>c.Sort).ToList();
            //获取字段
            var res = new JsonResult();
            res.Data = list;
            res.MaxJsonLength = 102400000;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取，否则用GET获取是会报错。 
            return res;

        }
        public JsonResult GetContentList()
        {
            IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> list = homeService.GetHomepageConfig().OrderBy(c => c.SortNo).ToList();
            //获取字段
            var res = new JsonResult();
            res.Data = list;
            res.MaxJsonLength = 102400000;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取，否则用GET获取是会报错。 
            return res;

        }

    }
}
